package com.dareway.juc.util;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class timeUtil {
    public static void main(String[] args) throws InterruptedException {
        Lock testLock = new ReentrantLock();
        Condition condition = testLock.newCondition();
        synchronized (timeUtil.class) {
            System.out.println("开始等待");
            TimeUnit.SECONDS.timedWait(timeUtil.class, 3);   //直接等待3秒
            System.out.println("等待结束");
        }
        TimeUnit.SECONDS.sleep(1);  //休眠1秒钟
        new Thread(() -> {
            testLock.lock();
            try {
                System.out.println("等待是否未超时："+condition.await(2, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            testLock.unlock();
        }).start();
        new Thread(()->{
            testLock.lock();
            System.out.println("唤醒");
            condition.signal();
            testLock.unlock();
        }).start();
    }
}
